package com.usefullc.crawler.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.usefullc.common.enums.ValidActionEnum;
import com.usefullc.crawler.param.ActiveOperateParam;
import com.usefullc.system.param.IdParam;
import com.usefullc.system.validator.UsefulValid;
import com.usefullc.crawler.domain.Active;
import com.usefullc.crawler.param.ActiveParam;
import com.usefullc.crawler.vo.ActiveVo;
import com.usefullc.crawler.query.ActiveQuery;
import com.usefullc.crawler.service.ActiveService;
import com.usefullc.common.util.BeanUtils;
import com.usefullc.system.controller.BaseController;
import com.usefullc.system.controller.WebResultBuilder;
import com.usefullc.system.entity.Pager;
import com.usefullc.system.entity.WebResult;
import com.usefullc.system.param.IdsParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import javax.validation.Valid;

import static com.usefullc.system.controller.WebResultBuilder.successResult;

/**
 * 爬虫活动控制类
 * @author: 星空
 * @date: 2022-07-27
 */
@RestController
@RequestMapping("/crawler/active/")
@Api( tags="爬虫活动")
public class ActiveController extends BaseController {

    @Autowired
    private ActiveService activeService;

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "爬虫活动列表",response = WebResult.class)
    @GetMapping(path = "list",produces = "application/json")
    public WebResult list(@UsefulValid(query = true) ActiveQuery query) {
        query.setExcludeColumns("analyseBanExpress,analyseExpress,analyseDataExpress,switchProxyExpress,waitCssSelectorExpress,uiEventScript");
        Pager<ActiveVo> page = activeService.findVoByPage(query);
        return WebResultBuilder.successResult(page);
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "爬虫活动所有列表", response = WebResult.class)
    @GetMapping(path = "listAll", produces = "application/json")
    public WebResult listAll(@UsefulValid(query = true) ActiveQuery query) {
        query.setExcludeColumns("analyseBanExpress,analyseExpress,analyseDataExpress,switchProxyExpress,waitCssSelectorExpress,uiEventScript");
        List<Active> dataList = activeService.findAll(query);
        return WebResultBuilder.successResult(dataList);
    }


    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "爬虫活动详情",response = WebResult.class)
    @GetMapping(path = "detail",produces = "application/json")
    public WebResult detail(@ApiParam(value = "示例ID") @Valid @RequestParam Long id) {
        Active active = activeService.findById(id);
        return WebResultBuilder.successResult(active);
    }


    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "创建爬虫活动", response = WebResult.class)
    @PostMapping(path = "/save", produces = "application/json")
    @UsefulValid
    public WebResult save(@UsefulValid(save=true) @RequestBody ActiveParam param) {
        Active active = BeanUtils.beanCopy(param, Active.class);
        activeService.saveEntity(active);

        return successResult();
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "修改爬虫活动", response = WebResult.class)
    @PostMapping(path = "/update", produces = "application/json")
    public WebResult update(@UsefulValid(update=true) @RequestBody ActiveParam param) {
        Active oldBean = activeService.findById(param.getId());
        BeanUtils.beanCopy(param, oldBean);
        return successResult(activeService.updateEntity(oldBean));
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @PostMapping(path = "del",produces = "application/json")
    @ApiOperation(value = "删除爬虫活动",response = WebResult.class)
    public WebResult del(@UsefulValid(del=true) @RequestBody IdsParam idParam) {
        activeService.delete(idParam.getIds());
        return WebResultBuilder.successResult();
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @PostMapping(path = "delByLogic",produces = "application/json")
    @ApiOperation(value = "逻辑删除爬虫活动",response = WebResult.class)
    public WebResult delByLogic(@UsefulValid(del=true) @RequestBody IdsParam idParam) {
        activeService.delete(idParam.getIds());
        return WebResultBuilder.successResult();
    }


    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "复制爬虫活动", response = WebResult.class)
    @PostMapping(path = "/copy", produces = "application/json")
    @UsefulValid
    public WebResult copy(@UsefulValid(save=true) @RequestBody IdParam idParam) {
        activeService.copy(idParam.getId());
        return successResult();
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "修改爬虫活动状态", response = WebResult.class)
    @PostMapping(path = "/updateStatus", produces = "application/json")
    public WebResult updateStatus(@UsefulValid(update=true) @RequestBody ActiveOperateParam param) {
        activeService.updateStatus(param.getId(),param.getStatus());
        return successResult();
    }




}
